package com.spzx.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spzx.product.domain.ProductSku;
import com.spzx.product.mapper.ProductSkuMapper;
import com.spzx.product.query.SkuQuery;
import com.spzx.product.service.IProductSkuService;
import com.spzx.product.api.domain.ProductSkuVo;
import com.spzx.product.vo.SkuPriceVo;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 商品sku 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2025-04-28
 */
@Service
public class ProductSkuServiceImpl extends ServiceImpl<ProductSkuMapper, ProductSku> implements IProductSkuService {

    @Override
    public List<ProductSkuVo> selectTopSale() {
        return baseMapper.selectTopSale();
    }

    /**
     * 分页条件查询商品列表
     *
     * @param skuQuery 查询条件：关键字、分类ID、品牌ID、排序方式
     * @return
     */
    @Override
    public List<ProductSkuVo> selectSkuList(SkuQuery skuQuery) {

        return baseMapper.selectSkuList(skuQuery);

    }

    //获取商品价格
    @Override
    public SkuPriceVo getSkuPrice(Long skuId) {
        LambdaQueryWrapper<ProductSku> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProductSku::getId, skuId).select(ProductSku::getSalePrice, ProductSku::getMarketPrice, ProductSku::getId);
        ProductSku productSku = baseMapper.selectOne(queryWrapper);
        if (productSku != null) {
            SkuPriceVo skuPriceVo = new SkuPriceVo();
            skuPriceVo.setSkuId(skuId);
            skuPriceVo.setMarketPrice(productSku.getMarketPrice());
            skuPriceVo.setSalePrice(productSku.getSalePrice());
            return skuPriceVo;
        }
        return null;
    }

    /**
     * 封装商品规格参数跟SKUID映射关系Map
     *
     * @param productId
     * @return
     */
    @Override
    public Map<String, Long> getSkuSpecValue(Long productId) {
        //1.创建保存映射关系Map
        HashMap<String, Long> specMap = new HashMap<>();
        //2.根据商品ID查询商品sku列表
        LambdaQueryWrapper<ProductSku> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ProductSku::getProductId, productId).select(ProductSku::getId, ProductSku::getSkuSpec);
        List<ProductSku> productSkuList = list(lambdaQueryWrapper);
        //3.遍历SKU列表 把SKU的规格参数 封装到Map中
        if (!CollectionUtils.isEmpty(productSkuList)) {
            for (ProductSku sku : productSkuList) {
                specMap.put(sku.getSkuSpec(),sku.getId());
            }
        }


        return specMap;
    }
    //获取商品库存

}
